//
//  HWHomeMoreTypeCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/27.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWHomeMoreTypeCell: UITableViewCell {
    
    var tagCollectionView:TTGTagCollectionView!
    var tagViews:[UIView] = Array()
    var model:HWHomeModel?{
        didSet {
            tagViews.removeAll()
            
            for (idx,mod) in (model?.fenlei ?? []).enumerated() {
                if idx < (model?.game?.count ?? 0) {
                    continue
                }
                tagViews.append(newButtonWithTitle(title: mod.name, fontSize: 11*widthScale ,tag: mod.id))

            }
            tagCollectionView.reload()
        }
    }
    var clickTagView:((_:String,_:String)->())?
    
    class func cellWithTableView(tableView:UITableView) -> (HWHomeMoreTypeCell) {
        let reUse:String = "HWHomeMoreTypeCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWHomeMoreTypeCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWHomeMoreTypeCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.white
        
        tagCollectionView = TTGTagCollectionView.init(frame: .zero)
        tagCollectionView.delegate = self
        tagCollectionView.dataSource = self
        tagCollectionView.manualCalculateHeight = true
        tagCollectionView.verticalSpacing = 9*widthScale
        tagCollectionView.horizontalSpacing = 16*widthScale
        tagCollectionView.preferredMaxLayoutWidth = screenWidth-26*widthScale
        contentView.addSubview(tagCollectionView)
    }
    
    func initFrame() -> Void {
        tagCollectionView.snp.makeConstraints { make in
            make.left.equalTo(13*widthScale)
            make.right.equalTo(-13*widthScale)
            make.top.equalTo(9*widthScale)
            make.bottom.equalTo(-10*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension HWHomeMoreTypeCell:TTGTextTagCollectionViewDelegate,TTGTagCollectionViewDelegate, TTGTagCollectionViewDataSource{
    func tagCollectionView(_ tagCollectionView: TTGTagCollectionView!, sizeForTagAt index: UInt) -> CGSize {
        return tagViews[Int(index)].frame.size
    }
    
    func numberOfTags(in tagCollectionView: TTGTagCollectionView!) -> UInt {
        return UInt(tagViews.count)
    }
    
    func tagCollectionView(_ tagCollectionView: TTGTagCollectionView!, tagViewFor index: UInt) -> UIView! {
        if index % 5 == 0 || index % 5 == 4{
            UIColor.gradientColor(view: tagViews[Int(index)], colors: [UIColor.colorWithAlphaHexString(hexString: "#3ED5FF", alpha: 0.1).cgColor,
                                                                       UIColor.colorWithAlphaHexString(hexString: "#3377FF", alpha: 0.2).cgColor],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 0, y: 1))
        }
        
        if index % 5 == 1 {
            UIColor.gradientColor(view: tagViews[Int(index)], colors: [UIColor.colorWithAlphaHexString(hexString: "#1CD67E", alpha: 0.1).cgColor,
                                                                       UIColor.colorWithAlphaHexString(hexString: "#139256", alpha: 0.2).cgColor],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 0, y: 1))
        }
        
        if index % 5 == 2 {
            UIColor.gradientColor(view: tagViews[Int(index)], colors: [UIColor.colorWithAlphaHexString(hexString: "#FF726A", alpha: 0.1).cgColor,
                                                                       UIColor.colorWithAlphaHexString(hexString: "#FF726A", alpha: 0.2).cgColor],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 0, y: 1))
        }
        
        if index % 5 == 3 {
            UIColor.gradientColor(view: tagViews[Int(index)], colors: [UIColor.colorWithAlphaHexString(hexString: "#FFD043", alpha: 0.1).cgColor,
                                                                       UIColor.colorWithAlphaHexString(hexString: "#DE6713", alpha: 0.2).cgColor],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 0, y: 1))
        }
        
        
        
        return tagViews[Int(index)]
    }
    
    
    func newButtonWithTitle(title:String,fontSize:CGFloat,tag:NSInteger) -> UIButton {
        let button = UIButton(type: .custom)
        button.setTitle(title, for: .normal)
        button.backgroundColor = .white
        button.titleLabel?.font = .systemFont(ofSize: fontSize)
        button.setTitleColor(.colorWithHexString(hexString: "#3E3E3E"), for: .normal)
        button.layer.masksToBounds = true
        button.sizeToFit()
        button.layer.cornerRadius = 15*widthScale
        expandSizeForView(view: button, extraWidth: 40*widthScale, extraHeight: 10*widthScale)
        button.tag = tag
        button.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        return button
    }
    
    func expandSizeForView(view:UIView,extraWidth:CGFloat,extraHeight:CGFloat) -> Void {
        var frame = view.frame
        frame.size.width = (screenWidth-(94*widthScale))/5
        frame.size.height = 30*widthScale
//        frame.size.height += extraHeight
        view.frame = frame
    }
    
    @objc func buttonClick(_ button:UIButton) -> Void {
        self.clickTagView?(button.titleLabel?.text ?? "" ,"\(button.tag)")
    }
}
